#include "BitmapFont.h"
#include "..\dxstdafx.h"
#include "..\utils\Log.h"

namespace ds {

BitmapFont::BitmapFont(const char* handle,uint32 startChar,uint32 charHeight,uint32 startX,uint32 startY,uint32 textureSize) 
	: Asset(handle) , m_CharHeight(charHeight) , m_StartChar(startChar) , m_StartX(startX) , m_StartY(startY) , m_TextureSize(textureSize) {
}

BitmapFont::~BitmapFont() {
	LOGC(logINFO,"BitmapFont") << "Removing bitmap font " << getName();	
}

void BitmapFont::addChar(uint32 ascii,int startX,int startY,int width) {
	CharDef cd;
	cd.ascii = ascii;
	cd.startX = startX;
	cd.startY = startY;
	cd.width = width;
	m_CharDefinitions.append(cd);
}

Rect BitmapFont::getTextureRect(uint32 ascii) {
	float top = 0.0f;
	float left = 0.0f;
	float height = 1.0f;
	float width = 1.0f;
	if ( ascii >= m_StartChar ) {
		uint32 idx = ascii - m_StartChar;
		CharDef* cd = &m_CharDefinitions[idx];			
		left = (float)cd->startX/(float)m_TextureSize;
		width = ((float)cd->startX +(float)cd->width)/(float)m_TextureSize - left;
		//LOGC(logINFO,"BitmapFont") << "getRect idx " << idx << " left " << left << " width " << width;
		top = (float)cd->startY/(float)m_TextureSize;
		height = ((float)cd->startY+(float)m_CharHeight)/(float)m_TextureSize - top;
	}
	return Rect(top,left,width,height);
}

float BitmapFont::getCharHeight() {
	return (float)m_CharHeight;
}

}
